Creating Your First Macromedia Central Application
Table of Contents
Turning a Simple Application into Something Useful
Although the product you've developed so far is nothing more than
an exercise, it's easy enough to modify so it does something more useful.
Specifically, you can make the agent treat the value received in its forwardMethod as
a ZIP code and look up the temperature using a web service. Without
detailing everything about Central's support for SOAP, simply modify
agent.as to read as follows:
owner=this;
agentMethod=function(param){
return "I heard "+param;
}
forwardMethod=function(param){
clearInterval(weatherInterval);
zipCode=param;
myService.tellEveryone("getting temperature for "+param);
getTempFunction();
}
getTempFunction =function(){
clearInterval(weatherInterval);
myRequest=myWebServiceObject.getTemp(zipCode);
myRequest.onResult=function(theResult) {
myService.tellEveryone(theResult);
weatherInterval=setInterval(getTempFunction,5000)
}
myRequest.onFault=function(theFault) {
tellEveryone("error "+theFault.faultstring);
}
}
onActivate = function(agentManager, agentID, data) {
gShell=agentManager;
myService=Central.LCService.createServer(myInterface, owner, true);
myWSDLUri=
"http://www.xmethods.net/sd/2001/TemperatureService.wsdl";
myWebServiceObject = new WebService(myWSDLUri);
}
onDeactivate = function(){
clearInterval(weatherInterval);
}
Central.initAgent (this, this);
Obviously, there's more code in this example. But the LCService and
two methods exposed to the clients (agentMethod and forwardMethod)
are still there. The forwardMethod is updated to trigger
the custom function getTempFunction(). There, a web service's getTemp() method
is triggered and the two possible result handlers (onResult or onFault)
are defined to pass the data back to the clients (using tellEveryone)
and set up a new interval that repeats the process in five seconds.
For more information about using web services in Macromedia Central,
see the documentation. Notice that because you're using setInterval() there's
a responsibility to use clearInterval() before beginning
an new interval.
In order to see this thing run, launch the application and check for updates. When that's complete, enter a ZIP code in the main application, click the Send button, and then—every five seconds or so—see the current temperature appear in both the pod and application. Actually, you can close the application and still view the pod—even close and reopen the pod! The five-second interval is only for testing purposes. Your real application should be more conservative, perhaps checking the web service only every few minutes.
Note: If you have problems with this portion, the completed files are in the folder "part4_finished--Turning a Simple Application into Something Useful."
Where to Go from Here
This tutorial emphasizes a few habits that you'll learn to appreciate.
One thing you should always do is keep the reference to the appropriate
shell (gShell in all my examples). It's your key to the
entire API for either the shell, Console, or Agent Manager. That is,
you can talk to Central through either of them.
When Central wants to talk to your application—say, to tell you that
the user is resizing the window—it looks for specific event handler
functions defined right in your main movie. That is, onActivate() is
how Central tells your application, pod, or agent that it's fully loaded.
When you study the API for the Application, Pod, or Agent objects,
realize that these are methods you put into your main movie with the
expectation that, at the right time, Central triggers them. You can
certainly call any of these functions yourself—it's a good way to consolidate
code—but for me it was easiest to learn the API once I broke down the
six main objects in this way (application, pod, and agent are managed
by the shell, Console, and Agent Manager).
Although I explained the LCService earlier in the "Creating an Agent" section, it may take you a little while to appreciate it fully. Basically, it consolidates code in the agent as well as lets the agent trigger code in the pods and application (or lets the pods and application trigger code in the agent).
There are many more cool features inside Macromedia Central. Let me give you a preview of my favorites:
- LCDataProvider is a way to declare a data provider
in one place and then access and modify the data in any piece of
your product. When the data changes, every file using the data (and
any component displaying the data) reflects the change immediately.
A data provider is basically an array full of identically formatted
ActionScript objects (such as records in a database where each one
has fields for first name, last name, and number). Many Flash components
can subscribe to a data provider to keep themselves in perfect sync.
(If you want to use traditional data providers with the Central component,
be sure to instantiate them using new
Central.DataProviderClass(). - Local Internet Cache is adjacent to Central where you can force any data file to cache. This way, users can still view images (including progressive JPEGs and GIFs) later. This also includes query results—say, the results of an ASP request. There are extensive tools to check whether a file exists already, remove it, or overwrite it. By default the cache size is 20 MB but users are automatically prompted to increase this when they exceed that amount.
- Notices are almost like little e-mail messages that appear in a common place on the Console. Users can set these so they chime when they arrive. Hopefully, developers won't send so many notices that they become spam, but they can be quite useful. For example, in my real estate application, users specify the sort of house they're looking for. Using an agent, Central periodically checks for new houses that match their profile. When one is available a notice pops up. Developers can control how long a notice stays up and also write scripts that react to users interacting with the notice.
- Blasting is a way for one application to prepare data (usually based on what the user selects) and send it to another application, which then processes the data in its own way. This means there's a sending application and a receiving application. One application might gather stock prices, for example, and then blast those numbers to a graphing tool. This cooperative nature means your application can focus on being really good at one thing instead of trying to be an "everything app."
Setting up the sending application is different than setting up the
receiving application. In either case, for two applications to pass
data they need to agree on a common data type. The sending application
prepares the data to send (by creating a SelectedItem object)
and then sets a "selection" (using setSelection()). It's
not until the user selects "Blast!" (from the highlighted Blast menu
appearing at the bottom right of the shell) that the data is sent.
The sending application just prepares the data and makes it available
to blast. (The exception to this is when user have autoblast turned
on, in which case data is sent as soon as a selection is set.)
The receiving application, in order to receive the data, first has
to declare that it accepts the same data type being sent. It also defines
an onSelectedItem() handler. This is the method that Central
calls when data is sent.
Although your first Central application will probably fail to earn you a million dollars, you should now have a good handle on the main pieces of a Central application. Specifically, you learned the following:
- How product.xml identifies which files get installed and easily updates existing installed components
- How to initialize an application (and pod and agent)
- How the
onActivate()callback is where you can begin doing Central stuff, as well as where you get a reference to the appropriate shell—Shell, Console, or Agent Manager, depending on whether you're in the application, pod, or agent file, respectively - How the LCService class simplifies sending messages between your application pieces
- How to set the publish directory in Flash to target the cache in Central, among other tips